package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.base.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {
	
	private static final Logger logger = Logger.getLogger(WayBill.class);
	
	@Autowired
	private WayBillService wayBillService;
	
	@Action(value="waybill_save",results={
			@Result(name="success",type="json")
	})
	//快速生成订单的操作
	public String save(){
		Map<String,Object> map=new HashMap<>();
		try{
			
		  wayBillService.save(model);
		  map.put("success", true);
		  map.put("msg","保存运单成功");
		  logger.info("保存运单成功,运单号是:"+model.getWayBillNum());
		  
		  
		}catch(Exception e){
			map.put("success", false);
			map.put("msg","保存运单失败");
			logger.error("保存运单失败,运单号是:"+model.getWayBillNum());
			
		}
		ServletActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	@Action(value="wayBill_pageQuery",results={
			@Result(name="success",type="json")
	})
	//无套件查询的方法
	public String pageQuery(){
	Pageable pageable=new PageRequest(page-1,rows,new Sort(new Sort.Order(Sort.Direction.DESC,"id")));
	//调用业务层进行查询
	Page<WayBill> pageData=wayBillService.pageQuery(pageable);
	//压入值栈返回
	pushToValueStack(pageData);
	return SUCCESS;
 }
}